package fun.sanguo.service.record.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import fun.sanguo.caches.UserCache;
import fun.sanguo.mapper.record.RecordPersonageMapper;
import fun.sanguo.model.dto.*;
import fun.sanguo.model.dto.interact.InteractPersonageDislikeResult;
import fun.sanguo.model.dto.interact.InteractPersonageLikeResult;
import fun.sanguo.model.dto.record.*;
import fun.sanguo.model.entity.record.RecordPersonageEntity;
import fun.sanguo.model.enums.ResEnums;
import fun.sanguo.service.interact.InteractPersonageDislikeService;
import fun.sanguo.service.interact.InteractPersonageLikeService;
import fun.sanguo.service.record.RecordPersonageService;
import fun.sanguo.service.sys.SysUserService;
import fun.sanguo.utils.DateUtils;
import fun.sanguo.utils.IdUtils;
import fun.sanguo.utils.exception.ArtificialException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 内容管理-人物列表 Service实现类
 *
 * @author ZYP
 * @date 2023-04-23
 */
@Service
public class RecordPersonageServiceImpl implements RecordPersonageService {

    @Resource
    private RecordPersonageMapper recordPersonageMapper;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private InteractPersonageLikeService interactPersonageLikeService;

    @Resource
    private InteractPersonageDislikeService interactPersonageDislikeService;

    /**
     * 查询页面table列表
     *
     * @param pageQuery
     * @return
     * @throws ArtificialException
     */
    @Override
    public PageInfo<RecordPersonageResult> getPage(PageQuery<RecordPersonageParam> pageQuery) throws ArtificialException {
        // 查询参数
        RecordPersonageEntity recordPersonageEntity = new RecordPersonageEntity();
        // 转换查询参数
        try {
            // 接收参数
            RecordPersonageParam recordPersonageParam = new ObjectMapper().convertValue(pageQuery.getParam(), RecordPersonageParam.class);
            // 赋值到实体类
            BeanUtils.copyProperties(recordPersonageParam, recordPersonageEntity);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 查询数据并封装返回
        try {
            // 查询总量
            int total = recordPersonageMapper.getTotal(recordPersonageEntity);
            // 查询列表（分页）
            List<RecordPersonageEntity> recordPersonageEntityList = recordPersonageMapper.getList(recordPersonageEntity, pageQuery);
            // 查询人物互动统计
            Map<String, InteractPersonageLikeResult> likeMap = interactPersonageLikeService.getSummaryByUser(sysUserService.getCurrentUserIdOrEmpty());
            Map<String, InteractPersonageDislikeResult> dislikeMap = interactPersonageDislikeService.getSummaryByUser(sysUserService.getCurrentUserIdOrEmpty());
            // 遍历组装
            List<RecordPersonageResult> recordPersonageResultList = new ArrayList<>();
            recordPersonageEntityList.forEach(entity -> {
                recordPersonageResultList.add(new RecordPersonageResult(entity, likeMap.get(entity.getId()), dislikeMap.get(entity.getId())));
            });
            // 封装并返回页面信息
            return new PageInfo<>(recordPersonageResultList, total, pageQuery);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 新增
     *
     * @param pageInsert
     * @return
     * @throws ArtificialException
     */
    @Override
    public boolean insert(PageInsert<RecordPersonageInsert> pageInsert) throws ArtificialException {
        // 新增参数
        RecordPersonageEntity recordPersonageEntity = new RecordPersonageEntity();
        // 转换新增参数
        try {
            // 接收参数
            RecordPersonageInsert recordPersonageInsert = new ObjectMapper().convertValue(pageInsert.getParam(), RecordPersonageInsert.class);
            // 赋值到实体类
            BeanUtils.copyProperties(recordPersonageInsert, recordPersonageEntity);
            // 其他参数赋值
            recordPersonageEntity.setId(String.valueOf(new IdUtils().nextId()));
            recordPersonageEntity.setCreateBy("system");
            recordPersonageEntity.setCreateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
            recordPersonageEntity.setUpdateBy("system");
            recordPersonageEntity.setUpdateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
            recordPersonageEntity.setVersion(1);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 新增数据并得到操作结果
        try {
            // 新增
            int amount = recordPersonageMapper.insert(recordPersonageEntity);
            // 返回操作结果
            return amount > 0;
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 编辑
     *
     * @param pageUpdate
     * @return
     * @throws ArtificialException
     */
    @Override
    public boolean update(PageUpdate<RecordPersonageUpdate> pageUpdate) throws ArtificialException {
        // 编辑参数
        RecordPersonageEntity recordPersonageEntity = new RecordPersonageEntity();
        // 转换编辑参数
        try {
            // 接收参数
            RecordPersonageUpdate recordPersonageUpdate = new ObjectMapper().convertValue(pageUpdate.getParam(), RecordPersonageUpdate.class);
            // 赋值到实体类
            BeanUtils.copyProperties(recordPersonageUpdate, recordPersonageEntity);
            // 其他参数赋值
            recordPersonageEntity.setUpdateBy("system");
            recordPersonageEntity.setUpdateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 编辑数据并得到操作结果
        try {
            // 编辑
            int amount = recordPersonageMapper.update(recordPersonageEntity);
            // 返回操作结果
            return amount > 0;
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 删除
     *
     * @param pageUpdate
     * @return
     * @throws ArtificialException
     */
    @Override
    public boolean delete(PageUpdate<RecordPersonageUpdate> pageUpdate) throws ArtificialException {
        // 删除参数
        RecordPersonageEntity recordPersonageEntity = new RecordPersonageEntity();
        // 转换删除参数
        try {
            // 接收参数
            RecordPersonageUpdate recordPersonageUpdate = new ObjectMapper().convertValue(pageUpdate.getParam(), RecordPersonageUpdate.class);
            // 赋值到实体类
            recordPersonageEntity.setId(recordPersonageUpdate.getId());
            // 其他参数赋值
            recordPersonageEntity.setUpdateBy("system");
            recordPersonageEntity.setUpdateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 删除数据并得到操作结果
        try {
            // 删除
            int amount = recordPersonageMapper.delete(recordPersonageEntity);
            // 返回操作结果
            return amount > 0;
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 查询详情
     *
     * @param dataQuery
     * @return
     * @throws ArtificialException
     */
    @Override
    public DataInfo<RecordPersonageDetailResult> getDetail(DataQuery<RecordPersonageDetailParam> dataQuery) throws ArtificialException {
        // 查询结果
        RecordPersonageDetailResult recordPersonageDetailResult = new RecordPersonageDetailResult();
        // 查询参数
        String id;
        try {
            // 接收参数
            RecordPersonageDetailParam recordPersonageDetailParam = new ObjectMapper().convertValue(dataQuery.getParam(), RecordPersonageDetailParam.class);
            // 参数赋值
            id = recordPersonageDetailParam.getId();
            // 判断参数是否为空
            if (StringUtils.isEmpty(id)) {
                throw new Exception("查询ID不能为空");
            }
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 查询数据并封装返回
        try {
            // 查询人物详情
            RecordPersonageEntity recordPersonageEntity = recordPersonageMapper.getById(id);
            // 替换 updateBy
            recordPersonageEntity.setUpdateBy(UserCache.getUsernameById(recordPersonageEntity.getUpdateBy()));
            // 封装结果
            recordPersonageDetailResult.setRecordPersonageEntity(recordPersonageEntity);
            // 封装并返回页面信息
            return new DataInfo<>(recordPersonageDetailResult);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }
}
